<?php
/**
 * 外发
 */
namespace app\mp\admin;

use app\system\admin\Admin;
use think\Db;
use app\common\service\Order as OrderService;
use app\common\service\OrderSize as OrderSizeService;

class Outgo extends Admin
{

    public function initialize()
    {
        parent::initialize();
    }

	/**
	 * 待外发
	 *
	 * @author KING
	 * @date 2020年7月1日
	 *
	 * @return string
	 */
    public function todo()
    {
        if ($this->request->isPost()) {
        	$result = [];
        	$where = [];
        	$where[] = ['status', 'in', [0, 1]];
        	$where[] = ['outgo_status', '>', 0];
        	$where[] = ['factory_outgo_id', '=', 0];

            //订单加一级
            order_level($where,'');
        	$list = db('order')->where($where)->order('shipment_time asc')->select();

        	$skus = array_column($list, 'sku');
        	$skuKV = db('sku')->where('sku', 'in', $skus)->column('ori_pic', 'sku');
        	$factoryOutgoKV = getFactoryOutgoKV(null);

        	foreach ($list as $v) {
        		$key = 'caigou';
        		if ($v['caigou_status'] == 1 && $v['caichuang_status'] == 0) $key = 'caichuang';
        		elseif ($v['caichuang_status'] == 1 && $v['chejian_status'] == 0) $key = 'chejian';

        		$result['list'][$v['factory_id']][$v['group_id']][$key][] = [
        			'client_ordernum' => $v['client_ordernum'],
        			'sku' => $v['sku'],
        			'quantity' => $v['quantity'],
        			'shipment_time' => date("Y-m-d", $v['shipment_time']),
        			'pic' => arrayGet($skuKV, $v['sku'], ''),
        			'tags' => getOrderTag($v),
        		];
        		if (isset($result['list'][$v['factory_id']][$v['group_id']][$key.'_orders'])) {
        			$result['list'][$v['factory_id']][$v['group_id']][$key.'_orders']++;
        		} else {
        			$result['list'][$v['factory_id']][$v['group_id']][$key.'_orders']=1;
        		}

        		if (isset($result['list'][$v['factory_id']][$v['group_id']][$key.'_items'])) {
        			$result['list'][$v['factory_id']][$v['group_id']][$key.'_items']+=$v['quantity'];
        		} else {
        			$result['list'][$v['factory_id']][$v['group_id']][$key.'_items']=$v['quantity'];
        		}

        	}

        	$result['factoryKV'] = [0 => '未分配'] + getFactoryKV(true);
        	$result['groupKV'] = [0 => '未分组'] + getGroupKV();
        	$result['factoryOutgoKV'] = getFactoryOutgoKV();

        	return $this->Json(1, '', $result);
        }

        return $this->fetch();
    }

    /**
     * 设置外发工厂
     *
     * @author KING
     * @date 2020年7月2日
     *
     */
    public function setFactoryOutgoId() {
    	$clientOrdernum = $this->request->post('client_ordernum');
    	$factoryOutgoId = $this->request->post('factory_outgo_id');
    	$data['outgo_status'] = 2;
    	$data['factory_outgo_id'] = $factoryOutgoId;
    	$data['outgo_time'] = $data['update_time'] = time();
    	$res = (new OrderService())->updateInfo($data, ['client_ordernum' => $clientOrdernum]);
    	$factoryOutgoKV = getFactoryOutgoKV(null);
    	$this->addOrderLog(0, $clientOrdernum, "设置外发工厂为[" . arrayGet($factoryOutgoKV, $factoryOutgoId, '')."]");
    	return $this->Json(1, '设置订单外发成功！');
    }

    /**
     * 已外发
     *
     * @author KING
     * @date 2020年7月1日
     *
     * @return string
     */
    public function done()
    {
    	if ($this->request->isPost()) {
    		$result = [];
    		$where = [];
    		$where[] = ['outgo_status', '>', 0];
    		$where[] = ['factory_outgo_id', '>', 0];
    		$where[] = ['outgo_over_time', '=', 0];

            //订单加一级
            order_level($where,'');
    		$list = db('order')->where($where)->order('shipment_time asc')->select();

    		$orderSizeService = new OrderSizeService();

    		$skus = array_column($list, 'sku');
    		$skuKV = db('sku')->where('sku', 'in', $skus)->column('ori_pic', 'sku');
    		$factoryOutgoKV = getFactoryOutgoKV(null);

    		foreach ($list as $v) {
    			$key = 'caigou';
    			if ($v['caigou_status'] == 1 && $v['caichuang_status'] == 0) $key = 'caichuang';
    			elseif ($v['caichuang_status'] == 1 && $v['chejian_status'] == 0) $key = 'chejian';
    			elseif ($v['chejian_status'] == 1 && $v['tail_status'] == 0) $key = 'tail';

    			if ($key == 'tail') {
    				$v['quantity'] = $orderSizeService->getShicaiCount($v['id']);	// 尾部取裁床数
    				$result[$key][] = [
    					'client_ordernum' => $v['client_ordernum'],
    					'sku' => $v['sku'],
    					'quantity' => $v['quantity'],
    					'shipment_time' => date("Y-m-d", $v['shipment_time']),
    					'factory_outgo_name' => arrayGet($factoryOutgoKV, $v['factory_outgo_id'], ''),
    					'pic' => arrayGet($skuKV, $v['sku'], ''),
    					'tags' => getOrderTag($v),
    				];
    				if (isset($result[$key.'_orders'])) {
    					$result[$key.'_orders']++;
    				} else {
    					$result[$key.'_orders']=1;
    				}

    				if (isset($result[$key.'_items'])) {
    					$result[$key.'_items']+=$v['quantity'];
    				} else {
    					$result[$key.'_items']=$v['quantity'];
    				}

    			} else {
    				$v['quantity'] = $key=='chejian' ? ($orderSizeService->getShicaiCount($v['id'])) : $v['quantity']; // 车间时取裁床数
    				$result['list'][$v['factory_id']][$v['group_id']][$key][] = [
    					'client_ordernum' => $v['client_ordernum'],
    					'sku' => $v['sku'],
    					'quantity' => $v['quantity'],
    					'shipment_time' => date("Y-m-d", $v['shipment_time']),
    					'factory_outgo_name' => arrayGet($factoryOutgoKV, $v['factory_outgo_id'], ''),
    					'pic' => arrayGet($skuKV, $v['sku'], ''),
    					'tags' => getOrderTag($v),
    				];
    				if (isset($result['list'][$v['factory_id']][$v['group_id']][$key.'_orders'])) {
    					$result['list'][$v['factory_id']][$v['group_id']][$key.'_orders']++;
    				} else {
    					$result['list'][$v['factory_id']][$v['group_id']][$key.'_orders']=1;
    				}

    				if (isset($result['list'][$v['factory_id']][$v['group_id']][$key.'_items'])) {
    					$result['list'][$v['factory_id']][$v['group_id']][$key.'_items']+=$v['quantity'];
    				} else {
    					$result['list'][$v['factory_id']][$v['group_id']][$key.'_items']=$v['quantity'];
    				}
    			}
    		}

    		$result['factoryKV'] = [0 => '未分配'] + getFactoryKV(true);
    		$result['groupKV'] = [0 => '未分组'] + getGroupKV();

    		return $this->Json(1, '', $result);
    	}

    	return $this->fetch();
    }

}